package com.jy.app.lib.base;


import android.content.Context;
import android.content.SharedPreferences;

import com.jy.app.lib.cache.CacheManager;


public  class BaseConfig {
	protected Context contextApp;
	protected SharedPreferences sp=null;
	protected SharedPreferences.Editor editor =null;
	private CacheManager cacheManager = CacheManager.getInstance();
 
	private BaseConfig() { }
	
	public BaseConfig(Context context, String sharedPreferencesName)
	{
		this.contextApp = context;
		this.sp = context.getSharedPreferences(sharedPreferencesName, 0);
		this.editor = sp.edit();
	}

	protected void setBoolean(String name,boolean bValue) {
		this.editor.putBoolean(name, bValue);
		this.editor.commit();
	}
	protected boolean getBoolean(String name,boolean defaultBool) {
		return sp.getBoolean(name, defaultBool);
	}
	
	protected void setString(String name,String strValue) {
		this.editor.putString(name, strValue);
		this.editor.commit();
	}
	protected String getString(String name,String defaultVal) {
		return sp.getString(name,defaultVal);
	}
	protected void setInt(String name,int  intValue) {
		this.editor.putInt(name, intValue);
		this.editor.commit();
	}
	protected int getInt(String name,int defaultVal) {
		return sp.getInt(name,defaultVal);
	}
	
	protected void setLong(String name,long longValue) {
		this.editor.putLong(name, longValue);
		this.editor.commit();
	}
	protected long getLong(String name,long defaultVal) {
		return sp.getLong(name,defaultVal);
	}
	
	protected void setBooleanWithCache(String name,boolean bValue) {
		setBoolean(name,bValue);
		cacheManager.put(name, bValue);
	}
	
	protected boolean getBooleanWithCache(String name,boolean defaultBool) 
	{
		if (cacheManager.get(name)==null) {
			boolean val = getBoolean(name, defaultBool);
			setBooleanWithCache(name, val);
			return val;
		}else {
			return (Boolean) cacheManager.get(name);
		}
	}
	
	protected void setStringWithCache(String name,String bValue) {
		setString(name,bValue);
		cacheManager.put(name, bValue);
	}
	
	protected String getStringWithCache(String name,String defaultVal) 
	{
		if (cacheManager.get(name)==null) {
			String val = getString(name, defaultVal);
			setStringWithCache(name, val);
			return val;
		}else {
			return (String) cacheManager.get(name);
		}
	}
	
	protected void setIntWithCache(String name,int intValue) {
		setInt(name, intValue);
		cacheManager.put(name, intValue);
	}
	
	protected int getIntWithCache(String name,int defaultVal) 
	{
		if (cacheManager.get(name)==null) {
			int val = getInt(name, defaultVal);
			setIntWithCache(name, val);
			return val;
		}else {
			return (Integer) cacheManager.get(name);
		}
	}
	
	protected void setLongWithCache(String name,long longValue) {
		setLong(name,longValue);
		cacheManager.put(name, longValue);
	}
	
	protected long getLongWithCache(String name,long defaultVal) 
	{
		if (cacheManager.get(name)==null) {
			long val = getLong(name, defaultVal);
			setLongWithCache(name, val);
			return val;
		}else {
			return (Long) cacheManager.get(name);
		}
	}

}
